package edu.pku.sei.act.classGenerator;

import edu.pku.sei.act.taskStruct.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

 public class TaskUIClassGenerator {  
  protected static String nl;
  public static synchronized TaskUIClassGenerator create(String lineSeparator)
  {
    nl = lineSeparator;
    TaskUIClassGenerator result = new TaskUIClassGenerator();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "";
  protected final String TEXT_2 = NL + "package edu.pku.sei.act.";
  protected final String TEXT_3 = ";" + NL + "" + NL + "import edu.pku.sei.act.actools.ITask;";
  protected final String TEXT_4 = "\t" + NL + "import edu.pku.sei.act.";
  protected final String TEXT_5 = ".";
  protected final String TEXT_6 = ".";
  protected final String TEXT_7 = ";";
  protected final String TEXT_8 = NL + "import android.os.Bundle;" + NL + "import android.os.IBinder;" + NL + "import android.os.RemoteException;" + NL + "import android.app.Activity;" + NL + "import android.content.ComponentName;" + NL + "import android.content.Context;" + NL + "import android.content.Intent;" + NL + "import android.content.ServiceConnection;" + NL + "import android.view.Menu;" + NL + "import android.view.View;" + NL + "import android.widget.Button;" + NL + "" + NL + "public class ";
  protected final String TEXT_9 = " extends Activity {" + NL + "\t" + NL + "\tprivate static ITask ";
  protected final String TEXT_10 = ";" + NL + "\t" + NL + "\tprivate ServiceConnection TaskConnection = new ServiceConnection() {" + NL + "" + NL + "\t\tpublic void onServiceConnected(ComponentName arg0, IBinder arg1) {" + NL + "\t\t\t";
  protected final String TEXT_11 = " = ITask.Stub.asInterface(arg1);" + NL + "\t\t}" + NL + "" + NL + "\t\tpublic void onServiceDisconnected(ComponentName name) {" + NL + "\t\t\t";
  protected final String TEXT_12 = " = null;" + NL + "\t\t}" + NL + "\t};" + NL + "" + NL + "    public void onCreate(Bundle savedInstanceState) {" + NL + "        super.onCreate(savedInstanceState);" + NL + "        setContentView(R.layout.activity_";
  protected final String TEXT_13 = ");" + NL + "        " + NL + "        Intent taskIntent = new Intent();" + NL + "        taskIntent.setAction(\"edu.pku.sei.act.";
  protected final String TEXT_14 = ".";
  protected final String TEXT_15 = "\");" + NL + "        bindService(taskIntent, TaskConnection, Context.BIND_AUTO_CREATE);";
  protected final String TEXT_16 = NL + "        Button ";
  protected final String TEXT_17 = " = (Button)findViewById(R.id.button1);" + NL + "        ";
  protected final String TEXT_18 = NL + "        ";
  protected final String TEXT_19 = ".setOnClickListener(new Button.OnClickListener() {" + NL + "" + NL + "\t\t\tpublic void onClick(View v) {" + NL + "\t\t\t\ttry {" + NL + "\t\t\t\t\t";
  protected final String TEXT_20 = ".playRole(\"";
  protected final String TEXT_21 = "\");" + NL + "\t\t\t\t} catch (RemoteException e) {" + NL + "\t\t\t\t\t" + NL + "\t\t\t\t\te.printStackTrace();" + NL + "\t\t\t\t}" + NL + "\t\t\t}" + NL + "        \t" + NL + "        });" + NL + "        ";
  protected final String TEXT_22 = NL + "    }" + NL + "" + NL + "    @Override" + NL + "\tprotected void onDestroy() {" + NL + "\t\tsuper.onDestroy();" + NL + "\t\tunbindService(TaskConnection);" + NL + "\t}" + NL + "" + NL + "    @Override" + NL + "    protected void onPause() {" + NL + "    \tunbindService(TaskConnection);" + NL + "    \tsuper.onPause();" + NL + "    }" + NL + "" + NL + "\t@Override" + NL + "    public boolean onCreateOptionsMenu(Menu menu) {" + NL + "        getMenuInflater().inflate(R.menu.activity_";
  protected final String TEXT_23 = ", menu);" + NL + "        return true;" + NL + "    }" + NL + "}";
  protected final String TEXT_24 = NL;

 public String generate(Task task)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
    
String taskName = NameGenerator.genTaskName(task.getName());
String taskProcessorName = NameGenerator.genTaskProcessorName(task.getName());
Collection<Role> roles = task.getRoles();

    stringBuffer.append(TEXT_2);
    stringBuffer.append( NameGenerator.genTaskPackageName(task.getName()));
    stringBuffer.append(TEXT_3);
     for(Iterator<Role> it = roles.iterator(); it.hasNext(); ) 
	{
		Role role = it.next();

    stringBuffer.append(TEXT_4);
    stringBuffer.append( NameGenerator.genTaskPackageName(task.getName()));
    stringBuffer.append(TEXT_5);
    stringBuffer.append( NameGenerator.genRolePackageName(role.getName()));
    stringBuffer.append(TEXT_6);
    stringBuffer.append( NameGenerator.genRoleName(role.getName()));
    stringBuffer.append(TEXT_7);
    	}
 
    stringBuffer.append(TEXT_8);
    stringBuffer.append( NameGenerator.genTaskUIName(task.getName()));
    stringBuffer.append(TEXT_9);
    stringBuffer.append( taskName);
    stringBuffer.append(TEXT_10);
    stringBuffer.append( taskName);
    stringBuffer.append(TEXT_11);
    stringBuffer.append( taskName);
    stringBuffer.append(TEXT_12);
    stringBuffer.append( taskName);
    stringBuffer.append(TEXT_13);
    stringBuffer.append( taskName);
    stringBuffer.append(TEXT_14);
    stringBuffer.append( taskProcessorName);
    stringBuffer.append(TEXT_15);
    
        for(Iterator<Role> it = roles.iterator(); it.hasNext(); )
        {
        	Role role = it.next();
        	String roleName = NameGenerator.genRoleName(role.getName());
        	String buttonName = "act_" + role.getName().toLowerCase(); 
        
    stringBuffer.append(TEXT_16);
    stringBuffer.append( buttonName);
    stringBuffer.append(TEXT_17);
    stringBuffer.append(TEXT_18);
    stringBuffer.append( buttonName);
    stringBuffer.append(TEXT_19);
    stringBuffer.append( taskName);
    stringBuffer.append(TEXT_20);
    stringBuffer.append( roleName);
    stringBuffer.append(TEXT_21);
    
        } 
        
    stringBuffer.append(TEXT_22);
    stringBuffer.append( taskName);
    stringBuffer.append(TEXT_23);
    stringBuffer.append(TEXT_24);
    return stringBuffer.toString();
  }
}